package com.example.demo.practice.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

import com.example.demo.practice.tool.SoundPlay;

import java.text.DecimalFormat;

/**
 * Created by Declan_Wang on 2018/2/9.
 */

public class MyTempView extends TextView {
    private Paint mPaint;//定义画笔
    private int height,width;//宽、高
    private int WHITE = Color.parseColor("#FFFFFF");
    private int GREEN = Color.parseColor("#33FF33");
    private int GREY = Color.parseColor("#999999");
    private int LIGHT = Color.parseColor("#99FFFFFF");//透明色亮块
    private int MORELIGHT = Color.parseColor("#A6FFFFFF");//更透明色亮块
    private int RED = Color.parseColor("#CC0000");

    private double temp;//温度
    private float fire;//报警温度

    public MyTempView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTempView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mPaint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.height = getMeasuredHeight();
        this.width = getMeasuredWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);
        drawCircle(canvas);
        drawRectF(canvas);
        drawInRectF(canvas,temp);
        drawInCircle(canvas,temp);
        drawDrackRectF(canvas,temp);
        drawLightCircle(canvas);
        drawLightRectF(canvas);
        drawMorelightCircle(canvas);
        drawTemp(canvas,temp);
    }

    /**
     * 画背景圆
     * @param canvas
     */
    private void drawCircle(Canvas canvas){
        mPaint.reset();//重置画笔设置
        mPaint.setColor(WHITE);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        canvas.drawCircle(width/2,height-width/8-2,width/8+2,mPaint);//画圆，圆心x、y位置，半径
    }

    /**
     * 画内圆
     * @param canvas
     */
    private void drawInCircle(Canvas canvas,double temp){
        mPaint.reset();//重置画笔设置
        if (temp > fire){
            mPaint.setColor(RED);//设置画笔颜色
        }else{
            mPaint.setColor(GREEN);//设置画笔颜色
        }
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        canvas.drawCircle(width/2,height-width/8-2,width/8-3,mPaint);//画圆，圆心x、y位置，半径
    }

    /**
     * 亮点圆
     * @param canvas
     */
    private void drawLightCircle(Canvas canvas){
        mPaint.reset();//重置画笔设置
        mPaint.setColor(LIGHT);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        canvas.drawCircle(width/2+width/32,height-width/8-2-width/32,width/20+5,mPaint);//画圆，圆心x、y位置，半径
    }

    /**
     * 更亮点圆
     * @param canvas
     */
    private void drawMorelightCircle(Canvas canvas){
        mPaint.reset();//重置画笔设置
        mPaint.setColor(MORELIGHT);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        canvas.drawCircle(width/2+width/22,height-width/8-2-width/17,width/52,mPaint);//画圆，圆心x、y位置，半径
    }

    /**
     * 画背景圆角矩形
     * @param canvas
     */
    private void drawRectF(Canvas canvas){

        mPaint.reset();//重置画笔设置
        mPaint.setColor(WHITE);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        RectF rectF = new RectF(width/2-width/16-2,height/40-2,width/2+width/16+2,height-width/8);//起始位置，结束位置
        canvas.drawRoundRect(rectF,width/60,width/60,mPaint);
    }

    /**
     * 画内圆角矩形
     * @param canvas
     */
    private void drawInRectF(Canvas canvas,double temp){

        mPaint.reset();//重置画笔设置
        if (temp > fire){
            mPaint.setColor(RED);//设置画笔颜色
        }else{
            mPaint.setColor(GREEN);//设置画笔颜色
        }
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        RectF rectF = new RectF(width/2-width/16+3,height/40+3,width/2+width/16-3,height-width/8);//起始位置，结束位置
        canvas.drawRoundRect(rectF,width/60,width/60,mPaint);
    }

    /**
     * 亮点条
     * @param canvas
     */
    private void drawLightRectF(Canvas canvas){

        mPaint.reset();//重置画笔设置
        mPaint.setColor(LIGHT);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        Rect rect = new Rect(width/2-3,height/40+20,width/2+width/16-18,height-width/4-20);//起始位置，结束位置
        canvas.drawRect(rect,mPaint);
    }

    /**
     * 暗条圆角矩形
     * @param canvas
     */
    private void drawDrackRectF(Canvas canvas,double temp){

        mPaint.reset();//重置画笔设置
        mPaint.setColor(GREY);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑

        float allHight = height - height/40 - 20 - (width/8+2)*2;

        RectF rectF = new RectF(width/2-width/16+3,height/40+3,width/2+width/16-3,(float)(height/40 + 20 + allHight * (100 - temp) / 100));//起始位置，结束位置
        canvas.drawRoundRect(rectF,width/60,width/60,mPaint);
    }

    /**
     * 绘制报警线
     * @param canvas
     */
    private void drawAlarm(Canvas canvas){
        mPaint.reset();//重置画笔设置
        mPaint.setColor(RED);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑
        mPaint.setTextSize(15f);//设置字体大小

        int allHight = height - height/40 - 20 - (width/8+2)*2;

        canvas.drawLine(width/2-width/16-102,height/40 + 20 + allHight * (100 - fire) / 100,width/2-width/16-17,height/40 + 20 + allHight * (100 - fire) / 100,mPaint);
        canvas.drawText("("+Float.toString(fire)+"°C)",width/2-width/16-162,height/40 + 23 + allHight * (100 - fire) / 100,mPaint);
    }

    /**
     * 绘制刻度
     * @param canvas
     */
    private void drawLine(Canvas canvas){
        mPaint.reset();//重置画笔设置
        mPaint.setColor(GREY);//设置画笔颜色
        mPaint.setAntiAlias(true);//抗锯齿，平滑
        mPaint.setTextSize(20f);//设置字体大小

        int allHight = height - height/40 - 20 - (width/8+2)*2;
        int ave = allHight/10;
        int temp = 100;

        for(int i=0;i<= 10;i++){
            canvas.drawLine(width/2-width/16-42,height/40 + 20 + ave*i,width/2-width/16-17,height/40 + 20 + ave*i,mPaint);
            canvas.drawText(Integer.toString(temp),width/2-width/16-85,height/40 + 30 + ave*i,mPaint);
            temp = temp - 10;
        }

        for(int i=0;i<= 9;i++){
            canvas.drawLine(width/2-width/16-32,height/40 + 20 + ave*i + ave/2,width/2-width/16-17,height/40 + 20 + ave*i + ave/2,mPaint);
        }

        drawAlarm(canvas);
    }

    /**
     * 设置温度
     */
    public void setTemp(double temp,float fire,SoundPlay soundPlay){
        if (temp > fire) {
            soundPlay.play();
        }
        this.temp = temp;
        this.fire = fire;
        this.invalidate();
    }

    /**
     * 绘制温度标记
     * @param canvas
     */
    private void drawTemp(Canvas canvas,double temp){
        mPaint.reset();//重置画笔设置
        if (temp > fire){
            mPaint.setColor(RED);//设置画笔颜色
        }else{
            mPaint.setColor(GREEN);//设置画笔颜色
        }
        mPaint.setAntiAlias(true);//抗锯齿，平滑
        mPaint.setTextSize(25f);//设置字体大小

        int allHight = height - height/40 - 20 - (width/8+2)*2;

        Path path = new Path();//多边形
        path.moveTo(width/2+width/16+13,(float)(height/40 + 20 + allHight * (100 - temp) / 100));//起始位置
        path.lineTo(width/2+width/16+53,(float)(height/40 + 20 + allHight * (100 - temp) / 100 - 20));
        path.lineTo(width/2+width/16+53,(float)(height/40 + 20 + allHight * (100 - temp) / 100 + 20));
        path.close();
        canvas.drawPath(path,mPaint);
        DecimalFormat dcmFmt = new DecimalFormat("0.0000");//保留四位小数
        canvas.drawText(dcmFmt.format(temp) + "°C",width/2+width/16+63,(float)(height/40 + 30 + allHight * (100 - temp) / 100),mPaint);
    }
}
